﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;

namespace WebApp.Models
{
    public class Photo
    {
        /// <summary>
        /// The ref class is to hold the representation of a photo that hasn't
        /// been populated yet. All fields are invalid until Populate() is
        /// called
        /// </summary>
        public class Ref : Photo
        {
            public Ref(String id)
            {
                this.id = id;
            }

            /// <summary>
            /// Populates the fields of this reference into a working photo
            /// object
            /// </summary>
            /// <param name="authToken">The token to use for 
            /// population</param>
            public void Populate(String authToken)
            {
                FacebookClient fbc = new FacebookClient(authToken);
                dynamic photo = fbc.Get(id.ToString());
                Populate(photo);
            }

            internal void Populate(dynamic photo)
            {
                id = photo["id"];

                String ownerId = photo["from"]["id"];
                String ownerName = photo["from"]["name"];
                owner = new Person.Ref(ownerId, ownerName);
                try
                {
                    name = photo["name"];
                }
                catch (Exception e)
                {
                }
                picture = photo["picture"];
                source = photo["source"];
                width = photo["width"];
                height = photo["height"];
                position = photo["position"];
                created = DateTime.Parse(photo["created_time"]);
                updated = DateTime.Parse(photo["updated_time"]);
            }
        }

        #region Fields

        private String id = null;
        private Person.Ref owner = null;
        private String name = null;
        private String picture = null;
        private String source = null;
        private long width = 0;
        private long height = 0;
        private long position = 0;
        private DateTime created = DateTime.Now;
        private DateTime updated = DateTime.Now;

        #endregion

        #region Properties

        /// <summary>
        /// The id of the photo
        /// </summary>
        public String Id
        {
            get { return id; }
        }

        /// <summary>
        /// The owner of the photo
        /// </summary>
        public Person.Ref Owner
        {
            get { return owner; }
        }

        /// <summary>
        /// The name/description of the photo
        /// </summary>
        public String Name
        {
            get { return name; }
        }

        /// <summary>
        /// The thumbnail image source of the photo
        /// </summary>
        public String Picture
        {
            get { return picture; }
        }

        /// <summary>
        /// The fullsize image source of the photo
        /// </summary>
        public String Source
        {
            get { return source; }
        }

        /// <summary>
        /// The width in pixels of the fullsized image
        /// </summary>
        public long Width
        {
            get { return width; }
        }

        /// <summary>
        /// The height in pixels of the fullsized image
        /// </summary>
        public long Height
        {
            get { return height; }
        }

        /// <summary>
        /// The position of the photo in its album
        /// </summary>
        public long Position
        {
            get { return position; }
        }

        /// <summary>
        /// The date the photo was uploaded
        /// </summary>
        public DateTime Created
        {
            get { return created; }
        }

        /// <summary>
        /// The date the photo was last updated
        /// </summary>
        public DateTime Updated
        {
            get { return updated; }
        }

        #endregion

        public Photo(String id, Person.Ref owner, String name, String picture,
            String source, long width, long height, long position, 
            DateTime created, DateTime updated)
        {
            this.id = id;
            this.owner = owner;
            this.name = name;
            this.picture = picture;
            this.source = source;
            this.width = width;
            this.height = height;
            this.position = position;
            this.created = created;
            this.updated = updated;
        }

        private Photo() { }
    }
}